<?php
class Tourism_cart_goods_model extends CI_Model
{
    private $table = 'tourism_cart_goods';
    private $table_2 = 'user';
    private $table_3 = 'tourism_goods_attr';
    private $table_4 = 'tourism_goods_base';
    private $table_6 = 'tourism_enshrine';
    
    public function findByGoodsAttrId($goodsAttrId, $uid)
    {
        $this->db->select("tourism_cart_goods.id,tourism_cart_goods.uid,tourism_cart_goods.goods_attr_id,tourism_goods_attr.goods_num,tourism_goods_base.limit_num");
        $this->db->from($this->table.' AS tourism_cart_goods');
        $this->db->join($this->table_3.' AS tourism_goods_attr', 'tourism_goods_attr.goods_attr_id = tourism_cart_goods.goods_attr_id','inner');
        $this->db->join($this->table_4.' AS tourism_goods_base', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id');
        $this->db->where('tourism_cart_goods.uid', $uid);
        $this->db->where('tourism_cart_goods.goods_attr_id', $goodsAttrId);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    public function findByUid($uid, $goodsAttrId)
    {
        $this->db->select("id,uid,goods_attr_id,goods_num,updated_at");
        $this->db->from($this->table);
        $this->db->where('uid', $uid);
        $this->db->where('goods_attr_id', $goodsAttrId);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    public function update($id, $data)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->table, $data);
    }

   public function updateGoodsNum($id, $number, $spec)
   {
        $this->db->set('goods_num', 'goods_num+'.$number, false);
        $this->db->set('attribute_value',$spec);
        $this->db->set('updated_at',date('Y-m-d H:i:s'));
        $this->db->where('id', $id);
        return $this->db->update($this->table);
    }

    public function insertCart($uid,$goodsAttrId, $attributeValue, $goodsNum)
    {
        $data = array(
            'uid'             => $uid,
            'goods_attr_id'   => $goodsAttrId,
            'attribute_value' => $attributeValue,
            'goods_num'       => $goodsNum,
            'updated_at'      => date('Y-m-d H:i:s')
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function delete($id,$uid)
    {
        $this->db->where_in('id', $id);
        $this->db->where('uid', $uid);
        return $this->db->delete($this->table);
    }

    public function deleteAttrId($goodsAttrId, $uid)
    {
        $this->db->where_in('goods_attr_id', $goodsAttrId);
        $this->db->where('uid', $uid);
        return $this->db->delete($this->table);
    }

    public function findTourismCart($uid)
    {
        $this->db->select('tourism_cart_goods.goods_attr_id,tourism_goods_attr.is_check,tourism_goods_attr.goods_num AS number,tourism_cart_goods.attribute_value,tourism_cart_goods.goods_num,tourism_cart_goods.id,
                    user.uid,user.alias_name,is_on_sale,tourism_goods_base.goods_name,tourism_goods_base.goods_id,tourism_goods_base.images,tourism_goods_base.rate_id,tourism_goods_attr.shop_price
                    ,tourism_enshrine.id AS "eid"');
        $this->db->from($this->table.' AS tourism_cart_goods');
        $this->db->join($this->table_3.' AS tourism_goods_attr','tourism_cart_goods.goods_attr_id = tourism_goods_attr.goods_attr_id');
        $this->db->join($this->table_4.' AS tourism_goods_base','tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->join($this->table_2.' AS user','tourism_goods_base.uid = user.uid');
        $this->db->join($this->table_6.' AS tourism_enshrine',"tourism_enshrine.goods_attr_id = tourism_goods_attr.goods_attr_id  AND tourism_enshrine.`uid` = {$uid} ",'left');
        $this->db->where('tourism_cart_goods.uid',$uid);
//        $this->db->where('tourism_cart_goods.goods_num >',0);
        $this->db->order_by('tourism_cart_goods.id','DESC');
        $this->db->group_by('tourism_cart_goods.goods_attr_id');
        return $this->db->get();
    }

    public function findTourismCartNum($uid, $attrIds)
    {
        $this->db->select('tourism_goods_base.limit_num, tourism_goods_base.limit_day, transport_cost, tourism_goods_base.rate_id, tourism_cart_goods.goods_attr_id,weight,tourism_goods_attr.goods_num AS number,tourism_goods_attr.market_price,tourism_goods_attr.supply_price,tourism_cart_goods.attribute_value,tourism_cart_goods.goods_num,tourism_cart_goods.id,
                    user.uid,user.user_name,user.alias_name,user.mobile_phone,tourism_goods_base.goods_name,tourism_goods_base.goods_id,tourism_goods_base.transport_id,tourism_goods_base.images,tourism_goods_attr.shop_price,tourism_goods_attr.swim');
        $this->db->from($this->table.' as tourism_cart_goods');
        $this->db->join($this->table_3.' as tourism_goods_attr','tourism_cart_goods.goods_attr_id = tourism_goods_attr.goods_attr_id');
        $this->db->join($this->table_4.' as tourism_goods_base','tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->join($this->table_2.' as user','tourism_goods_base.uid = user.uid');
        $this->db->where('tourism_cart_goods.uid', $uid);
        $this->db->where_in('tourism_cart_goods.goods_attr_id', $attrIds);
        $this->db->where('tourism_goods_base.is_on_sale', 1);
        $this->db->where('tourism_cart_goods.goods_num >', 0);
        $this->db->order_by('tourism_cart_goods.id','DESC');
        $this->db->group_by('tourism_cart_goods.goods_attr_id');
        return $this->db->get();
    }

    public function findCartDisabled($uid)
    {
        $this->db->select('tourism_cart_goods.id');
        $this->db->from($this->table.' AS tourism_cart_goods');
        $this->db->join($this->table_3.' AS tourism_goods_attr','tourism_cart_goods.goods_attr_id = tourism_goods_attr.goods_attr_id');
        $this->db->join($this->table_4.' AS tourism_goods_base','tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->where('tourism_cart_goods.uid', $uid);
        $this->db->where('(`tourism_goods_base`.`is_on_sale` <> 1 OR `tourism_goods_attr`.`is_check` <> 2 OR `tourism_goods_attr`.`goods_num` <= 0)');
        return $this->db->get();
    }
}